﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input;

namespace Deflection
{
    class PlayerFactory
    {
        private static PlayerFactory instance = null;


        List<Player> players = new List<Player>();
        public List<Player> Players
        {
            get { return players; }
            set { players = value; }
        }

        public List<Player> GetPlayers()
        {
            return players;
        }


        public static PlayerFactory Instance()
        {
            if (instance == null)
            {
                instance = new PlayerFactory();
            }
            return instance;
        }

        private PlayerFactory()
        {

        }

        public void Update(GameTime gameTime)
        {


            for (int i = 0; i < players.Count; i++)
            {
                players[i].Update(gameTime);
                if (players[i].playerState == Player.PlayerState.Alive)
                {
                    CheckCollisions(players[i]);
                }
            }
            //players.RemoveAll(FindDeadPlayers);

            // Check if all the players are completely dead
            if (players.All(p => p.playerState == Player.PlayerState.GameOver))
            {
                Console.WriteLine("ALL DEAD");
            }
        }

        private bool FindDeadPlayers(Player p)
        {
            if (!p.Alive)
            {
                return true;
            }
            return false;
        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach(Player player in players)
            {
                player.Draw(gameTime, spriteBatch);                
            }
        }

        public void LoadContent(ContentManager content)
        {
            Player.PlayerSprite = Player.LoadContent(content, "Sprites/player");
            Shield.ShieldSprite = Shield.LoadContent(content, "Sprites/shield");
            Player.Shoot1 = Player.LoadAudioContent(content, "Audio/Effects/Shoot1");
            Player.Shoot2 = Player.LoadAudioContent(content, "Audio/Effects/Shoot2");
            Player.Shoot3 = Player.LoadAudioContent(content, "Audio/Effects/Shoot3");
        }

        public void CreatePlayers()
        {

        }

        public void CreatePlayer(int playerID)
        {
            if (playerID == 1)
            {
                players.Add(new Player(playerID, GameParameters.PLAYER1_START_POSITION));
            }
            else
            {
                players.Add(new Player(playerID, GameParameters.PLAYER2_START_POSITION));
            }
        }
        public void CheckCollisions(Player player)
        {
            List<Enemy> enemies = EnemyFactory.Instance().Enemies;

            for (int e = 0; e < enemies.Count; e++)
            {
                if (enemies[e].Type != Enemy.EnemyType.SuperSquare && enemies[e].Type != Enemy.EnemyType.SuperTriangle && enemies[e].Type != Enemy.EnemyType.SuperHexagon && enemies[e].IsCollidable)
                {
                    if (player.CheckForCollision(enemies[e]))
                    {
                        player.playerState = Player.PlayerState.Dying;
                        enemies[e].State = Enemy.EnemyState.Dying;

                        Random rand = new Random((int)enemies[e].position.X);
                        int r = rand.Next(0, 100);
                        if (r <= 33)
                        {
                            ProjectileFactory.Explode1.Play(0.125f, 1.0f, 0.0f);

                        }
                        else if (r <= 66)
                        {
                            ProjectileFactory.Explode3.Play(0.125f, 1.0f, 0.0f);
                        }
                        else
                        {
                            ProjectileFactory.Explode2.Play(0.125f, -0.50f, 0.0f);
                        }
                    }
                }
                if (e < players.Count)
                {
                    if( players[e] != player && player.CheckForCollision(players[e]) && player.velocity != players[e].velocity)
                    {
                        Vector2 P1 = new Vector2(player.velocity.X, player.velocity.Y);
                        Vector2 P2 = new Vector2(players[e].velocity.X, players[e].velocity.Y);
                        player.velocity = P2;
                        players[e].velocity = P1;
                    }
                }
            }
        }
    }
}
